package com.example.logistics_manage.controller;

import com.example.logistics_manage.pojo.News;
import com.example.logistics_manage.result.restful.R;
import com.example.logistics_manage.result.restful.ResponseEunm;
import com.example.logistics_manage.service.INewsService;
import com.example.logistics_manage.service.serviceImpl.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author quhao
 * @since 1.0
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {
    @Autowired
    private INewsService newsService;

    @GetMapping("/selectAllNews")
    public R<List<News>> selectAllNews(){
        return new R<>(ResponseEunm.SUCCESS,newsService.selectAllNews());
    }

    @PostMapping("/updateNews")
    public R<News> updateNews(@RequestBody News news){
        newsService.updateNews(news);
        return new R<>(ResponseEunm.SUCCESS,null);
    }
    @GetMapping("/deleteNews/{id}")
    public R<News> deleteNews(@PathVariable("id")Integer id){
        newsService.deleteNews(id);
        return new R<>(ResponseEunm.SUCCESS,null);
    }

    @GetMapping("/selectByTitle")
    public R<List<News>> selectByTitle(@RequestParam("title") String title){
        return new R<>(ResponseEunm.SUCCESS,newsService.selectByTitle(title));
    }
    @PostMapping("/saveNews")
    public R<News> insertNews(@RequestBody News news){
        newsService.insertNews(news);
        return new R<>(ResponseEunm.SUCCESS,null);
    }
}
